﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Net.Mail;

namespace CIS470MetricsTracking
{
	public partial class UserRegistration : Form
	{
		public UserRegistration()
		{
			InitializeComponent();
		}

		private void btnRegister_Click(object sender, EventArgs e)
		{
			RegexUtilities util = new RegexUtilities();

			string first_name = txtFirstName.Text.ToString().Trim();
			string last_name = txtLastName.Text.ToString().Trim();
			string email = txtEmail.Text.ToString().Trim();
			string password = txtPassword.Text.ToString().Trim();
			string confirm_password = txtConfirmPassword.Text.ToString().Trim();
			string company_phone = txtCompanyPhone.Text.ToString().Trim();

			if (first_name == "" || last_name == "" || email.Equals("") || password == "" || confirm_password == "" || company_phone == "") {
				lblErrorText.Text = "All fields are required.";
				lblErrorText.Visible = true;
			} else if (!util.IsValidEmail(email)) {
				lblErrorText.Text = "Email must be a valid format.";
				lblErrorText.Visible = true;
			} else if (ControllerManager.user_controller.check_email(email)) {
				lblErrorText.Text = "That e-mail address is in use.";
				lblErrorText.Visible = true;
            } else if (!util.IsValidPhone(company_phone)) {
                lblErrorText.Text = "Phone number must be valid.";
                lblErrorText.Visible = true;
            } else if (ControllerManager.user_controller.check_phone(company_phone)) {
                lblErrorText.Text = "That phone number is in use.";
                lblErrorText.Visible = true;
            } else if (password.Length < 4 || password.Length > 12 || confirm_password.Length < 4 || confirm_password.Length > 12) {
				lblErrorText.Text = "Password must be between 4 and 12 characters long.";
				lblErrorText.Visible = true;
			} else if (password != confirm_password) {
				lblErrorText.Text = "Passwords do not match.";
				lblErrorText.Visible = true;
			} else {
				User user = new User(first_name, last_name, email, password,
					DateTime.Now.ToString("MM/dd/yyyy"), 2, company_phone);

				if (ControllerManager.user_controller.create_user(user)) {
					lblErrorText.Text = "Registration successful.  Please log in using the previous screen.";
					lblErrorText.Visible = true;
					btnRegister.Visible = false;
					btnClose.Text = "Close";
					btnClose.Visible = true;
				} else {
					lblErrorText.Text = "There was an error during registration.";
					lblErrorText.Visible = true;
				}
			}
		}

		private void btnClose_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void UserRegistration_Load(object sender, EventArgs e)
		{

		}
	}
}
